﻿using MediatR;
using S.ReportForms.Domain.Dto;
using S.ReportForms.Infrastructure.Interface;
using static S.ReportForms.Read.API.Application.Command.SheepSoredInRealTimeCommand;

namespace S.ReportForms.Read.API.Application.CommandHandlers
{
    public class SheepSoredInRealTimeCommandHandler : IRequestHandler<SheepSoredInRealTimeTotalCountCommand, SheepStoredInRealTimeDto>,IRequestHandler<SheepFatherDetailCommand,List<BreedFatherDetailDto>>,IRequestHandler<BreedChildCommand,List<BreedChildDto>>,IRequestHandler<BreedMotherCommand,List<BreedMotherDto>>,IRequestHandler<MotherDetailCommand,MotherPageDto>,IRequestHandler<FatherDetailCommand,FatherPageDto>
    {
        private readonly ISheepStoredInRealTimeRepository repository;

        public SheepSoredInRealTimeCommandHandler(ISheepStoredInRealTimeRepository repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 羊场存栏
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<SheepStoredInRealTimeDto> Handle(SheepSoredInRealTimeTotalCountCommand request, CancellationToken cancellationToken)
        {
            return repository.GetSheepStoredInRealTimeDto(request.BreedType);
        }
        /// <summary>
        /// 种公详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<BreedFatherDetailDto>> Handle(SheepFatherDetailCommand request, CancellationToken cancellationToken)
        {
            return await repository.GetBreedFatherDetail();
        }
        /// <summary>
        /// 后裔详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<List<BreedChildDto>> Handle(BreedChildCommand request, CancellationToken cancellationToken)
        {
            return await repository.GetBreedChild();
        }
        /// <summary>
        /// 获取种母信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<BreedMotherDto>> Handle(BreedMotherCommand request, CancellationToken cancellationToken)
        {
            return await repository.GetBreedMother();
        }
        /// <summary>
        /// 获取种母
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<MotherPageDto> Handle(MotherDetailCommand request, CancellationToken cancellationToken)
        {
            var list= await repository.GetMotherDto();
            int totalCount=list.Count();
            int pageCount = (int)(totalCount * 1.0) / request.pageSize;
            list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            MotherPageDto motherPageDto = new MotherPageDto()
            {
                mothersDetailDtos=list,
                totalCount=totalCount,
                pageCount=pageCount,
            };
            return motherPageDto;
        }
        /// <summary>
        /// 获取种公
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async  Task<FatherPageDto> Handle(FatherDetailCommand request, CancellationToken cancellationToken)
        {
            var list = await repository.GetFathersDto();
            int totalCount = list.Count();
            int pageCount = (int)(totalCount * 1.0) / request.pageSize;
            list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            FatherPageDto fatherPageDto = new FatherPageDto()
            {
                fathersDetailDtos = list,
                totalCount = totalCount,
                pageCount = pageCount,
            };
            return fatherPageDto;
        }
    }
}
